001 /*
002 * Copyright 2005 Stephen J. McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.transit.registry;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.OutputStream;
024 import java.net.URL;
025 import java.net.URLConnection;
026 import java.security.AccessController;
027 import java.security.PrivilegedExceptionAction;
028 import java.security.PrivilegedActionException;
029 import java.rmi.registry.Registry;
030 import java.rmi.registry.LocateRegistry;
031
032 /**
033 * The registry URL protocol connection implementation.
034 *
035 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
036 * @version 1.0.2
037 */
038 public class RegistryURLConnection extends URLConnection
039 {
040 private boolean m_connected = false;
041
042 private Registry m_registry;
043
044 /**
045 * Creation of a new registry handler.
046 * @param url the url to establish a connection with
047 * @exception NullPointerException if the url argument is null
048 */
049 RegistryURLConnection( URL url )
050 throws NullPointerException
051 {
052 super( url );
053 }
054
055 /**
056 * Establish a connection.
057 *
058 * @exception IOException is an error occurs while attempting to establish
059 * the connection.
060 */
061 public void connect()
062 throws IOException
063 {
064 if( m_connected )
065 {
066 return;
067 }
068
069 m_connected = true;
070
071 try
072 {
073 AccessController.doPrivileged(
074 new PrivilegedExceptionAction()
075 {
076 public Object run()
077 throws IOException
078 {
079 String spec = url.toExternalForm();
080 String host = url.getHost();
081 int port = url.getPort();
082
083 try
084 {
085 m_registry = LocateRegistry.getRegistry( host, port );
086 }
087 catch( Throwable e )
088 {
089 String message = e.getMessage();
090 IOException exception = new IOException( message );
091 exception.initCause( e.getCause() );
092 throw exception;
093 }
094 return null; // nothing to return
095 }
096 }
097 );
098 }
099 catch( PrivilegedActionException e )
100 {
101 throw (IOException) e.getException();
102 }
103 }
104
105 /**
106 * Return an input stream to the resource.
107 * @return current implementation returns null
108 * @exception IOException is an error occurs
109 */
110 public InputStream getInputStream()
111 throws IOException
112 {
113 return null;
114 }
115
116 /**
117 * Return an output stream to the resource.
118 * @return current implementation returns null
119 * @exception IOException if any I/O problems occur.
120 */
121 public OutputStream getOutputStream()
122 throws IOException
123 {
124 return null;
125 }
126
127 /**
128 * Return the content for this registry connection.
129 * @return the registry object
130 * @exception IOException is an error occurs
131 */
132 public Object getContent() throws IOException
133 {
134 return getContent( new Class[0] );
135 }
136
137 /**
138 * Return the content for this registry connection.
139 * @param classes a sequence of classes against which the
140 * implementation will attempt to establish a known match
141 * @return the registry object
142 * @exception IOException is an error occurs
143 */
144 public Object getContent( final Class[] classes ) throws IOException
145 {
146 connect();
147
148 try
149 {
150 return AccessController.doPrivileged(
151 new PrivilegedExceptionAction()
152 {
153 public Object run()
154 throws IOException
155 {
156 if( null == m_registry )
157 {
158 throw new NullPointerException( "registry" );
159 }
160 try
161 {
162 String path = getURL().getPath();
163 if( ( null == path ) || "".equals( path ) )
164 {
165 return m_registry;
166 }
167 else
168 {
169 return m_registry.lookup( path );
170 }
171 }
172 catch( Exception e )
173 {
174 final String error =
175 "Unable to resolve url: " + getURL();
176 IOException exception = new IOException( error );
177 exception.initCause( e );
178 throw exception;
179 }
180 }
181 }
182 );
183 }
184 catch( PrivilegedActionException e )
185 {
186 throw (IOException) e.getException();
187 }
188 }
189 }